package yt.kpool.activity.miner;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.web.ProfitWebAc;
import yt.kpool.adapter.AddMinerBottomListAdapter;
import yt.kpool.adapter.AddMinerTopListAdapter;
import yt.kpool.bean.AddMinerRemarkBean;
import yt.kpool.bean.LineChartTestBean;
import yt.kpool.bean.MinerCharBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;

import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 添加矿工的方法
 */
public class AddMinerRemarkActivity extends AppCompatActivity {

    @BindView(R.id.lv_TextList)
    ListView lv_TextList;
     @BindView(R.id.lv_MinerSetup)
    ListView lv_MinerSetup;
    @BindView(R.id.BottomText)
    TextView tv_BottomText;
    @BindView(R.id.cardview)
    CardView cardview;


    private List<AddMinerRemarkBean.DataBean.TextListBean> topList;
    private List<AddMinerRemarkBean.DataBean.MinerSetupBean> bottomList;

    protected AddMinerTopListAdapter addMinerTopListAdapter;
    protected AddMinerBottomListAdapter addMinerBottomListAdapter;

    /**
     * 跳转方法
     * @param mactivity
     * @param CurrencyId 币种

     */
    public static void start(Activity mactivity, String CurrencyId, String CurrencyCode)
    {
        Intent intent = new Intent(mactivity, AddMinerRemarkActivity.class);
        intent.putExtra("CurrencyId",CurrencyId);
        intent.putExtra("CurrencyCode",CurrencyCode);
        mactivity.startActivity(intent);
    }
    public String getCurrencyId() {
        return getIntent().getStringExtra("CurrencyId");
    }
    public String getCurrencyCode() {
        return getIntent().getStringExtra("CurrencyCode");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_miner_remark);
        ButterKnife.bind(this);
        initTitle();
        initLisView();
        getData();
    }

    private void getData(){
        if (NetWorkUtil.check(this)) {

        NetHelperNew.AddMinerRemark(getCurrencyId(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                Log.i("-->", "添加矿工的方法：" + data);

                AddMinerRemarkBean addMinerRemarkBean = MyGson.getInstance().fromJson(data, AddMinerRemarkBean.class);


                if (addMinerRemarkBean.getType() == 1) {
                   topList.addAll(addMinerRemarkBean.getData().getTextList());
                   addMinerTopListAdapter.notifyDataSetChanged();

                   if (addMinerRemarkBean.getData().getMinerSetup().size()==0)
                   {
                       cardview.setVisibility(View.GONE);
                   }

                   bottomList.addAll(addMinerRemarkBean.getData().getMinerSetup());
                   addMinerBottomListAdapter.notifyDataSetChanged();
                   tv_BottomText.setText(""+addMinerRemarkBean.getData().getBottomText());
                } else {

                    new ErrorDialog(AddMinerRemarkActivity.this, addMinerRemarkBean.getContent()).show();
                }
            }
            @Override
            public void onError(String msg) {
                super.onError(msg);


                ToastUtil.showToast(AddMinerRemarkActivity.this, msg);
            }
        });

    }
}

    private void initLisView() {
        topList=new ArrayList<>();
        bottomList=new ArrayList<>();
        addMinerTopListAdapter=new AddMinerTopListAdapter(this,topList);
        addMinerBottomListAdapter=new AddMinerBottomListAdapter(this,bottomList);
        lv_TextList.setAdapter(addMinerTopListAdapter);
        lv_MinerSetup.setAdapter(addMinerBottomListAdapter);


    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle(getCurrencyCode()+"矿工添加方法");

    }

    @OnClick({ R.id.btn_learn})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_learn:
                ProfitWebAc.start(AddMinerRemarkActivity.this, NetHelperNew.Tutorial + getCurrencyCode(), getCurrencyCode()+ "挖矿教程");
                break;
        }
    }
}
